<?php
// +----------------------------------------------------------------------
// | Author: Stupboy King
// +----------------------------------------------------------------------
namespace app\api\model;
use app\api\model\Common;
use think\Config;
use think\db;
class Driver extends Common
{
    public function order(){
        return $this->hasMany('Order','driver_id','driver_id');
    }
    
    public function user(){
        return $this->hasOne('User','user_id','user_id');
    }
    
    public function drivercard(){
        return $this->hasOne('DriverCard','driver_id','driver_id');
    }

    public function car(){
        return $this->hasOne('Car', 'car_id', 'car_id');
    }
    
    public function driveraccount(){
        return $this->hasOne('DriverAccount','driver_id','driver_id');
    }

    public function carrental(){
        return $this->hasOne('CarRental','car_rental_id','car_rental_id');
    }
    
    /**
     * 一对一关联提现表
     * @return type
     */
    public function Withdrawalsrecord(){
        return $this->hasOne('WithdrawalsRecord','driver_id','driver_id');
    }
    
    /**
     * 听单状态切换
    */
    public function changeStatus($driver_id=0,$status=3){
        return $this->save(['work_state' => $status],['driver_id' => $driver_id]);
    }

    /**
     * 设置定位
     * @param int $driver_id
     * @param int $lon
     * @param int $lat
     * @return false|int
     */
    public function setLocation($driver_id=0,$lon=0,$lat=0){
        return $this->save(['lon'=>$lon,'lat'=>$lat,'update_time'=>date('Y-m-d H:i:s')],['driver_id'=>$driver_id]);
    }

    /**
     * 首页数据
     * @param $driver_id
     * @return mixed
     */
    public function homeData($driver_id){
        $driver              = $this->get($driver_id);
        $data['xing']        = $driver->getAttr('star');
        $data['server']      = $driver->getAttr('score');
        $data['car_brand']   = $driver->car->carbrand ? $driver->car->carbrand->getAttr('name') : '';
        $data['car_no']      = $driver->car ? $driver->car->getAttr('number') : '';
        $data['car_model']   = $driver->car->cartype ? $driver->car->cartype->getAttr('car_type_name') : '';
        $data['rental_name'] = $driver->carrental ? $driver->carrental->getAttr('name') : '';
        $data['driver_name'] = $driver->getAttr('name');
        $data['phone']       = $driver->getAttr('phone');
        $data['sex']         = $driver->getAttr('sex');
        $data['state']       = $driver->getAttr('state');
        $data['withdraw_password'] = $driver->driveraccount ? $driver->driveraccount->getAttr('withdraw_password') : '';
        $start_time          = date('Y-m-d')." 00:00:00";
        $end_time            = date('Y-m-d')." 23:59:59";
        $result = Db::view('order',[])
        ->view('order_fee',[],'order.order_id=order_fee.order_id','left')
            ->view('order_gps',[],'order.order_id = order_gps.order_id','left')
        ->where('driver_id',$driver_id)
            ->where("plan_time between  '$start_time' and '$end_time' ")
        ->field('sum(driver_money) money,count(driver_id) orders')->find();
        $data['money']       = $result['money'] ? $result['money'] : 0;
        $data['orders']      = $result['orders'];
        $data['online_time'] = 2000;
        $data['receive_rate']= $driver->getAttr('rate');
        return $data;
    }

    /**
     * 修改密码
     * @param $driver_id
     * @param $old
     * @param $new
     * @return mixed
     */
    public static function changePassword($driver_id,$old,$new){
        $driver = self::get($driver_id);
        $salt   = $driver->user->getAttr('salt');
        return $driver->user->save(['password'=>CorePwd($new)],['password'=>CorePwd($old)]);
    }

    /**
     * 订单费用计算
     * @param $driver_id
     * @param $oid
     * @param int $time
     * @param int $meter
     * @param int $low
     * @param int $night
     * @return mixed
     */
    public static function countFee($driver_id,$oid,$time=0,$meter=0,$low=0,$night=0){
        $driver          = self::get($driver_id);
        $carType         = $driver->car->cartype;
        $starting_price  = $carType ? $carType->getAttr('starting_price') : 0;
        $Pstarting_price = $carType ? $carType->getAttr('p_starting_price') : 0;
        $starting_km     = $carType ? $carType->getAttr('starting_km') : 0;
        $mileage_cost    = $carType ? $carType->getAttr('mileage_cost') : 0;
        $Pmileage_cost   = $carType ? $carType->getAttr('p_mileage_cost') : 0;
        $transit_cost    = $carType ? $carType->getAttr('transit_cost') : 0;
        $Ptransit_cost   = $carType ? $carType->getAttr('p_transit_cost') : 0;
        $mileFee         = $meter/1000 - $starting_km > 0 ? ($meter/1000-$starting_km)*$mileage_cost : 0;
        $PmileFee        = $meter/1000 - $starting_km > 0 ? ($meter/1000-$starting_km)*$Pmileage_cost : 0;
        $remoteMile      = $meter/1000 - Config::get('base.remote_km');
        $remoteMile      = $remoteMile > 0 ? $remoteMile : 0;
        $remoteFee       = $remoteMile > 0 && $transit_cost >0 ? $remoteMile*$transit_cost :0;
        $PremoteFee      = $remoteMile > 0 && $Ptransit_cost>0 ? $remoteMile*$Ptransit_cost :0;
        $fee             = $starting_price + $mileFee + $remoteFee;
        $p_fee           = $Pstarting_price + $PmileFee + $PremoteFee;
        $data['start_fee']       = $Pstarting_price;
        $data['start_fee2']      = $starting_price;
        $data['base_time']       = 0;
        $data['remote_meter']    = $remoteMile;
        $data['remote_fee']      = $PremoteFee;
        $data['remote_fee2']     = $remoteFee;
        $data['base_meter']      = $starting_km;
        $data['use_time']        = $time;
        $data['time_fee']        = 0;
        $data['time_fee2']       = 0;
        $data['low_speed_time']  = $low;
        $data['low_fee']         = 0;
        $data['low_fee2']        = 0;
        $data['night_meter']     = $night;
        $data['night_fee']       = 0;
        $data['night_fee2']      = 0;
        $data['meter']           = $meter;
        $data['travel_fee2']     = $fee;
        $data['travel_fee']      = $p_fee;
        $data['passenger_money'] = $p_fee;
        $data['driver_money']    = $fee;
        $data['company_money']   = $p_fee - $fee;
        return $data;
    }
}
